package org.wenshu.ai.example;

import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.input.Prompt;
import dev.langchain4j.model.input.PromptTemplate;
import dev.langchain4j.model.input.structured.StructuredPrompt;
import dev.langchain4j.model.input.structured.StructuredPromptProcessor;
import dev.langchain4j.model.openai.OpenAiChatModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static dev.langchain4j.model.openai.OpenAiChatModelName.GPT_4_O_MINI;
import static java.time.Duration.ofSeconds;
import static java.util.Arrays.asList;

public class _03_PromptTemplate {

    static class Simple_Prompt_Template_Example {

        public static void main(String[] args) {

            ChatLanguageModel model = OpenAiChatModel.builder()
                .baseUrl("https://maas-api.cn-huabei-1.xf-yun.com/v1")
                .apiKey("your-key")
                .modelName("xdeepseekv3")
                    .timeout(ofSeconds(60))
                    .build();

            String template = "使用以下食材创建一个{{dishType}}的食谱：{{ingredients}}";
            PromptTemplate promptTemplate = PromptTemplate.from(template);

            Map<String, Object> variables = new HashMap<>();
            variables.put("dishType", "烤箱菜");
            variables.put("ingredients", "土豆、番茄、菲达奶酪、橄榄油");

            Prompt prompt = promptTemplate.apply(variables);

            String response = model.chat(prompt.text());

            System.out.println(response);
        }

    }

    static class Structured_Prompt_Template_Example {
        @StructuredPrompt({
                "使用仅有的{{ingredients}}制作一道{{dish}}。",
                "请按照以下格式组织回答：",

                "食谱名称：...",
                "描述：...",
                "准备时间：...",

                "所需食材：",
                "- ...",
                "- ...",

                "制作步骤：",
                "- ...",
                "- ..."
        })
        static class CreateRecipePrompt {

            String dish;
            List<String> ingredients;

            CreateRecipePrompt(String dish, List<String> ingredients) {
                this.dish = dish;
                this.ingredients = ingredients;
            }
        }

        public static void main(String[] args) {

            ChatLanguageModel model = OpenAiChatModel.builder()
                .baseUrl("https://maas-api.cn-huabei-1.xf-yun.com/v1")
                .apiKey("your-key")
                .modelName("xdeepseekv3")
                    .timeout(ofSeconds(60))
                    .build();

            CreateRecipePrompt createRecipePrompt = new CreateRecipePrompt(
                    "沙拉",
                    asList("黄瓜", "番茄", "菲达奶酪", "洋葱", "橄榄")
            );

            Prompt prompt = StructuredPromptProcessor.toPrompt(createRecipePrompt);

            String recipe = model.chat(prompt.text());

            System.out.println(recipe);
        }
    }
}
